//
//  UIButton+General.m
//  shopping
//
//  Created by 阿帕 on 2018/5/18.
//  Copyright © 2018年 GCY. All rights reserved.
//

#import "UIButton+General.h"
#import <objc/message.h>

@implementation UIButton (General)
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space
{
    //    self.backgroundColor = [UIColor cyanColor];
    
    /**
     *  前置知识点：titleEdgeInsets是title相对于其上下左右的inset，跟tableView的contentInset是类似的，
     *  如果只有title，那它上下左右都是相对于button的，image也是一样；
     *  如果同时有image和label，那这时候image的上左下是相对于button，右边是相对于label的；title的上右下是相对于button，左边是相对于image的。
     */
    
    
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case MKButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case MKButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}

@end


///////
@implementation UIButton (Initialize)

#pragma mark - getter方法
- (UIButton *(^)(NSString *, CGFloat, UIColor *))titleParams{
    return ^UIButton *(NSString *title,CGFloat fountSize,UIColor *textColor){
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:textColor forState:UIControlStateNormal];
        [self.titleLabel setFont:[UIFont systemFontOfSize:fountSize]];
        return self;
    };
}

- (UIButton *(^)(UIColor *))selfBackGroundColor{
    return ^UIButton *(UIColor *backGroundColor){
        self.backgroundColor = backGroundColor;
        return self;
    };
}

- (UIButton *(^)(UIImage *))selfImage{
    return ^UIButton *(UIImage *image){
        [self setImage:image forState:UIControlStateNormal];
        return self;
    };
}

#pragma mark - setter
- (void)setTitleParams:(UIButton *(^)(NSString *, CGFloat, UIColor *))titleParams{
    
}

- (void)setSelfBackGroundColor:(UIButton *(^)(UIColor *))selfBackGroundColor{
    
}

- (void)setSelfImage:(UIButton *(^)(UIImage *))selfImage{
    
}

@end


// 默认的按钮点击时间间隔
static const NSTimeInterval defaultDuration = 3.0f;

const char *repeatEventIntervalKey = "repeatEventIntervalKey";

const char *previousClickTimeKey = "previousClickTimeKey";

@implementation UIButton (repeat)

+ (void)load {
        
    // 交换方法实现
    
//    Method sendAction = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:));
//
//    Method xy_SendAction = class_getInstanceMethod([self class], @selector(xy_sendAction:to:forEvent:));
//
//    method_exchangeImplementations(sendAction, xy_SendAction);
    
}

// 重写，为了防止在tabBarController下点击tabBarItem时报错
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    
    [super sendAction:action to:target forEvent:event];
    
}

- (void)setRepeatEventInterval:(NSTimeInterval)repeatEventInterval {
    
    objc_setAssociatedObject(self, repeatEventIntervalKey, @(repeatEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval)repeatEventInterval {
    
    NSTimeInterval repeatEventIn = (NSTimeInterval)[objc_getAssociatedObject(self, repeatEventIntervalKey) doubleValue];
    
    // 如果外界设置的重复点击的时间间隔大于0，就按照用户设置的去处理，如果用户设置的间隔时间小于或等于0，就按照无间隔处理
    
    if (repeatEventIn > 0) {
        
        return repeatEventIn;
        
    } else {
        
        return defaultDuration;
        
    }
    
    return 0.0;
    
}


- (void)setPreviousClickTime:(NSTimeInterval)previousClickTime {
    
    objc_setAssociatedObject(self, previousClickTimeKey, @(previousClickTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval)previousClickTime {
    
    NSTimeInterval previousEventTime = [objc_getAssociatedObject(self, previousClickTimeKey) doubleValue];
    
    if (previousEventTime != 0) {
     
        return previousEventTime;
    }
    
    return 1.0;
    
}



- (void)xy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {

    NSTimeInterval time = [[[NSDate alloc] init] timeIntervalSince1970];
    
    if (time - self.previousClickTime < self.repeatEventInterval) {
        
        return;
        
    }

    // 如果间隔时间大于0
    
    if (self.repeatEventInterval > 0) {
        
        self.previousClickTime = [[[NSDate alloc] init] timeIntervalSince1970];
        
    }
    
    // 已在load中与系统的sendAction:to:forEvent:方法交换方法实现，所以下面调用的还是系统的方法
    
    [self xy_sendAction:action to:target forEvent:event];
    
}

@end
